package com.marchsoft.musicalstore.service;

import com.marchsoft.musicalstore.entity.Instrument;
import com.marchsoft.musicalstore.repository.InstrumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 源
 * Date: 2019-11-21
 * Time: 21:19
 */
@Service
public class InstrumentSer {
    @Autowired
    InstrumentRepository instrumentRepository;

    /**
     * 0代表此商品还在售卖
     * @param type
     * @param page
     * @param limit
     * @return
     */
    public List<Instrument> getInstruByTypeLimit(Integer type, Integer page, Integer limit){
        Pageable mypage = PageRequest.of(page - 1, limit);
        List<Instrument> byState;
        if(type==0){
            byState = instrumentRepository.findByState(0, mypage);
        }else{
            byState = instrumentRepository.findByStateAndType(0,type,mypage);
        }
        return byState;
    }
    public Optional<Instrument> getInstruById(Integer id){
        Optional<Instrument> byId = instrumentRepository.findById(id);
        return byId;

    }
}
